package net.ikenway.springboot.demo.Model.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/10/2020
 */
@Entity
@DynamicUpdate
@Data
@Table(name = "orders")
@NoArgsConstructor
@AllArgsConstructor
public class Order implements Serializable {
    public enum Type {
        TAKEOUT,
        DINE
    }

    public enum Status {
        TOPAY,
        PAYED,
        CANCELING,
        CANCELLED,
        FINISHED
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long orderId;

    @ManyToOne(targetEntity = User.class, fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "user_id")
    private User user;

    @OneToMany(targetEntity = OrderSub.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<OrderSub> orderSubs;

    private BigDecimal totalPrice;

    private Timestamp createTime;

    private Timestamp purchaseTime;

    @Enumerated(EnumType.STRING)
    private Type type;

    @Enumerated(EnumType.STRING)
    private Status status = Status.TOPAY;

    @OneToOne(targetEntity = Address.class, fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "address_id")
    private Address address;
}
